package com.hb.trade.api.manager.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.hb.core.pojo.Attachment;
import com.hb.core.pojo.Attachments;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.portal.dto.PortalNoticeDTO;
import com.hb.core.pojo.project.portal.entity.PortalNoticeFile;
import com.hb.trade.client.project.PortalNoticeClient;
import fzzn.core.aspose.convert.AsposeConvertUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.MsgUtil;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 后台管理 - 公告管理
 *
 * @author wxj
 */
@Slf4j
@Version(1.0)
@Api(tags = {"公告管理相关接口"})
@RequestMapping("/{version}")
@RestController
public class ApiPortalNoticeController {

    @Resource
    private PortalNoticeClient portalNoticeClient;

    @PostMapping("/portalNotice/pagePortalNoticeList4Manager")
    public ResponseDto pagePortalNoticeList(@RequestBody PortalNoticeDTO portalNoticeDTO){
        Message<PageResult<PortalNoticeDTO>> result = portalNoticeClient.pagePortalNoticeList4Manager(portalNoticeDTO);

        return ResultUtil.msg(result);
    }


    @PostMapping("/portalNotice/findNoticeById")
    public ResponseDto findNoticeById(@RequestBody PortalNoticeDTO portalNoticeDTO){
        Message<PortalNoticeDTO> result = portalNoticeClient.findNoticeById(portalNoticeDTO);

        return ResultUtil.msg(result);
    }

    @PostMapping("/portalNotice/savePortalNotice")
    public ResponseDto savePortalNotice(@RequestBody PortalNoticeDTO portalNoticeDTO){
        if (portalNoticeDTO != null){
            portalNoticeDTO.setDeployTime(LocalDateTime.now());
            portalNoticeDTO.setPortalNoticeType(1);
        }
        // 保存公告内容
        Attachments content = portalNoticeDTO.getNoticeContentFile();
        if (CollectionUtil.isNotEmpty(content)){
            Attachment noticeContent = content.get(0);
            File thisFile = noticeContent.getThisFile();
            String htmlStr = AsposeConvertUtil.wordToHtml(thisFile);

            portalNoticeDTO.setNoviceContent(htmlStr);
        }
        // 保存公告附件
        Attachments attachments = portalNoticeDTO.getAttachments();
        if (CollectionUtil.isNotEmpty(attachments)){
            List<PortalNoticeFile> portalNoticeFileList = attachments.stream().map(attachment -> {
                PortalNoticeFile portalNoticeFile = new PortalNoticeFile();
                portalNoticeFile.setFileName(attachment.getRawFileName());
                portalNoticeFile.setFilePath(attachment.getFileUrl());
                return portalNoticeFile;
            }).collect(Collectors.toList());

            portalNoticeDTO.setPortalNoticeFileList(portalNoticeFileList);
        }

        Message<Boolean> result = portalNoticeClient.savePortalNotice4Manager(portalNoticeDTO);

        return ResultUtil.msg(result);
    }

    @PostMapping("/portalNotice/delPortalNotice")
    public ResponseDto delPortalNotice(@RequestBody PortalNoticeDTO portalNoticeDTO){

        Message<Boolean> result = portalNoticeClient.delPortalNotice(portalNoticeDTO);

        return ResultUtil.msg(MsgUtil.success(MsgEnum.SUCCESS));
    }
}
